﻿//Functions used for validating duplicate value while inserting
function ValidateWhileInserting(src, dest) {
    var ctrl = document.getElementById(src);
    // call server side method
    PageMethods.ValidateWhileInserting(ctrl.value, CallSuccess, CallFailed, dest);
}

//Functions used for validating duplicate value while Update operation
function ValidateWhileUpdating(src, dest) {
    //Retrieve, Value to validate and Primary key identifier's value
    var arr = src.split('$$')
    var valueCtrl = document.getElementById(arr[0]);
    var idCtrl = document.getElementById(arr[1]);

    // call server side method
    PageMethods.ValidateWhileUpdating(valueCtrl.value, idCtrl.value, CallSuccess, CallFailed, dest);
}

//Functions used for validating duplicate value while inserting
function ValidateFieldWhileInserting(src, dest, fieldToValidate) {
    var ctrl = document.getElementById(src);
    // call server side method
    PageMethods.ValidateWhileInserting(fieldToValidate, ctrl.value, CallSuccess, CallFailed, dest);
}

//Functions used for validating duplicate value while Update operation
function ValidateFieldWhileUpdating(src, dest, fieldToValidate) {
    //Retrieve, Value to validate and Primary key identifier's value
    var arr = src.split('$$')
    var valueCtrl = document.getElementById(arr[0]);
    var idCtrl = document.getElementById(arr[1]);

    // call server side method
    PageMethods.ValidateWhileUpdating(fieldToValidate, valueCtrl.value, idCtrl.value, CallSuccess, CallFailed, dest);
}

// set the error msg and disable submit button
function CallSuccess(res, destCtrl) {
    //destCtrl contains concatenated IDs for the Label ctrl,Submit btn ctrl and Error Message to display
    //Split these IDs on '$$' delimeter
    var arr = destCtrl.split('$$')
    var lblCtrl = document.getElementById(arr[0]);
    var submitBtn = document.getElementById(arr[1]);
    var errorMsg = arr[2];
    if (res == 'Y') {
        lblCtrl.value = errorMsg
        submitBtn.disabled = "disabled"
    }
    else {
        lblCtrl.value = ""
        submitBtn.disabled = ""
    }
}

// function to alert message on some failure
function CallFailed(res, destCtrl) {
    alert(res.get_message());
}

